<#escape x as (x)!"">

var vm = new Vue({
	el:'#rrapp',
	data:{
	    q:{
            searchName: null,
            sidx: null,
            order: null
        },
        page:{total:0,limit:30,page:1},
        dataList:[],
        isadd: false,
		showList: true,
		showView: false,
		title: null,
        addFlag: false,
        editFlag: false,
		${classname}: {},

        columnsList: [
          <#if columns??>
              <#list columns as column>
                <#if !column.nolist>
          {name: '${column.shortComments}', field: '${column.attrname}' , isShow: true},
                </#if>
              </#list>
          </#if>
        ],
	<#if columns??>
		<#list columns as column>
		<#if column.map??>
        ${column.attrname}List: [
        <#list column.map?keys as key> 
        <#if column.attrType?? && column.attrType=="Integer">
        {'value': ${key}, 'text': '${column.map[key]}'}<#if key_has_next>,</#if>
        <#elseif column.attrType?? && column.attrType=="Long">
        {'value': ${key}, 'text': '${column.map[key]}'}<#if key_has_next>,</#if>
        <#else>
        {'value': '${key}', 'text': '${column.map[key]}'}<#if key_has_next>,</#if>
        </#if>
        </#list>
        ],
        ${column.attrname}Map: {},
        ${column.attrname}SearchList: {},
		</#if>
		</#list>
	</#if>
		rules: {
	<#if columns??>
		<#list columns as column>
                    <#if !column.noinput && (column.nullable=="N" || column.nullable=="NO") >
                      <#if column.inputType?? &&(column.inputType=="select" || column.inputType=="radio" || column.inputType=="date")>
          ${column.attrname}: [
            {required: true, message: '请选择${column.shortComments}', trigger: 'change'}
          ],
                      <#else>
          ${column.attrname}: [
            {required: true, message: '请输入${column.shortComments}', trigger: 'blur'},
            {min: 2, max: 20, message: '长度在 2 到 20 个字', trigger: 'blur'}
          ],
                      </#if>
                    </#if>
          </#list>
          test:[]
       </#if> 
       }
	},
    mounted: function () {
    	<#if columns??>
    		<#list columns as column>
    		<#if column.map??>
        this.${column.attrname}Map = list2Map(this.${column.attrname}List)
    		</#if>
    		</#list>
    	</#if>
        this.getDataList();
    },
	methods: {
		query: function () {
			vm.reload();
		},
        getDataList:function () {
            this.q.page = this.page.page;
            this.q.limit = this.page.limit;

            $.ajax({
                type: "POST",
                url: "../${pathName}/list",
                data: JSON.stringify(this.q),
                success: function (r) {
                    if (r.code == 0) {
                        //debugger;
                        vm.dataList = r.data.list;
                        vm.page.total = r.data.totalCount;
                    } else {
                        toastr.error(r.message);
                    }
                }
            });
        },
		add: function(){
		    vm.isadd = true; 
			vm.showList = false;
			vm.showView = false;
			vm.title = "新增";
			vm.${classname} = {};
		},
		update: function (${pk.attrname}) {
			if(${pk.attrname} == null){
				return ;
			}
			vm.showList = false;
			vm.showView = false;
            vm.title = "修改";
            vm.getInfo(${pk.attrname})
		},
        view2update: function () {
            vm.showList = false;
            vm.showView = false;
            vm.title = "修改";
        },
		view: function(${pk.attrname}){
        		vm.title = "查看";
	        vm.getInfo(${pk.attrname})
	        	vm.showList = false;
	        	vm.showView = true;
        },
        changeToUpdate:function(){
            vm.showView = false;
        },
		saveOrUpadateValid:function (valid){
            if (valid) {
                var url = vm.isadd ? "../${pathName}/save" : "../${pathName}/update";
                $.ajax({
				type: "POST",
			    url: url,
			    data: JSON.stringify(vm.${classname}),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.message);
					}
				}
			});
            }else{
                alert('验证未通过');
            }
        },
		saveOrUpdate: function (event) {
			this.$refs['${classname}Form'].validate(vm.saveOrUpadateValid);
		},
		del: function (${pk.attrname}s) {

			if(${pk.attrname}s == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: "../${pathName}/delete",
				    data: JSON.stringify(${pk.attrname}s),
				    success: function(r){
						if(r.code == 0){
                            alert('删除成功', function(index){
                                vm.reload();
                            });
						}else{
							alert(r.message);
						}
					}
				});
			});
		},
		delOne: function (${pk.attrname}) {

            var ${pk.attrname}s = [];
            ${pk.attrname}s[0] = ${pk.attrname};

        	confirm('确定要删除该记录？', function(){
        		$.ajax({
        			type: "GET",
        			url: "../${pathName}/delete/"+${pk.attrname},
        			success: function(r){
                        if(r.code == 0){
                            alert('删除成功', function(index){
                                vm.reload();
                            });
                        }else{
                            alert(r.message);
                        }
        			}
        		});
        	});
        },
		getInfo: function(${pk.attrname}){
			$.get("../${pathName}/info/"+${pk.attrname}, function(r){
                vm.${classname} = r.data;
            });
		},
        handleCommand:function (commond) {
            if(commond=='d')
            {
                location.href = "../${pathName}/download?query=" + escape(JSON.stringify(vm.q));
                return;
            }

            if(vm.total>5000)
            {
                toastr.error("导出数据条数不能超过5000条,请用sql从数据库导出!");
                return;
            }

            if(commond=='a')
            {
                $("#data").tableExport({type:'csv'},'data');
                return;
            }

            if(commond=='b')
            {
                $("#data").tableExport({type:'txt'},'data');
                return;
            }

            if(commond=='c')
            {
                $("#data").tableExport({type:'excel',escape:false,
                    mso: {fileFormat:'xlsx',
                        excelstyles: ['border-bottom', 'border-top', 'border-left', 'border-right'],
                        worksheetName: ['Data']}},'data');
                return;
            }
        },
        exportExl: function () {
            if(vm.total>5000)
            {
                toastr.error("导出数据条数不能超过5000条,请用sql从数据库导出!");
                return;
            }

            if(vm.total>5000)
            {
                toastr.error("导出数据条数不能超过5000条,请用sql从数据库导出!");
                return;
            }

            $("#data").tableExport({type:'excel',escape:false,
                mso: {fileFormat:'xlsx',
                    excelstyles: ['border-bottom', 'border-top', 'border-left', 'border-right'],
                    worksheetName: ['Data']}},'data');

        },

        pageSizeChange:function (pageSize) {
            this.page.limit = pageSize;
            this.reload();
        },
        pageCurrentChange:function (page) {
            this.page.page = page;
            this.reload();
        },
        pageChange:function () {
            this.reload();
        },
		reload: function (event) {
			this.isadd = false;
			this.showList = true;
            this.getDataList();
		}
	}
});
</#escape>